package com.iraeta.service.basedata.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.iraeta.Config;
import com.iraeta.common.Constants;
import com.iraeta.common.ServiceException;
import com.iraeta.dao.basedata.ScviewrecordDao;
import com.iraeta.dao.basedata.ScviewrecordmxDao;
import com.iraeta.dao.sys.SysFilesDao;
import com.iraeta.entity.Scviewrecord;
import com.iraeta.entity.Scviewrecordmx;
import com.iraeta.entity.vo.ScviewrecordVo;
import com.iraeta.service.basedata.ScviewrecordService;
import com.iraeta.uitl.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;

@Service
public class ScviewrecordServiceImpl implements ScviewrecordService {
    @Autowired
    ScviewrecordDao scviewrecordDao;
    @Autowired
    ScviewrecordmxDao scviewrecordmxDao;
    @Autowired
    SysFilesDao sysFilesDao;
    @Autowired
    Config config;

    @Override
    public void setScviewrecord(Scviewrecord record, List<Scviewrecordmx> scviewrecordmxList) {
        try {
            String excelFilePath = config.profile + sysFilesDao.selectSysFilesByFId(record.getFileid().longValue()).getFilePath().substring(Constants.RESOURCE_PREFIX.length());
            FileInputStream fileInputStream = new FileInputStream(new File(excelFilePath));
            XSSFWorkbook excelFile = new XSSFWorkbook(fileInputStream);
            int sheets = excelFile.getNumberOfSheets();
            XSSFSheet xssfSheet = excelFile.getSheetAt(sheets - 1);
            String title = ExcelUtil.ReadData(xssfSheet, 0, 0);     // 2025/1月2日
            String titleDate = title.substring(0, 9);
            Date recordDate = DateUtil.parse(titleDate, "yyyy/MM月dd日");
            Scviewrecord scviewrecordDb = scviewrecordDao.selectByOrgid(record.getOrgid(), DateUtil.formatDate(recordDate));
            if(scviewrecordDb != null){
                throw new ServiceException(DateUtil.formatDate(recordDate) + "已上传生产计划");
            }
            record.setCdefine1(title);
            record.setCreatetime(new Date());
            record.setRecordtime(recordDate);

            int lastRowNum = xssfSheet.getLastRowNum();//获取有文字的最后一行行号
            for(int i=5; i<lastRowNum; ++i){
                XSSFRow row = xssfSheet.getRow(i);
                Scviewrecordmx scviewrecordmx = new Scviewrecordmx();
                scviewrecordmx.setRid(record.getId());
                if(StrUtil.isEmpty(row.getCell(0).getStringCellValue())){
                    break;
                }
                scviewrecordmx.setCccode1(row.getCell(0).getStringCellValue());
                scviewrecordmx.setCccount1(StrUtil.isEmpty(ExcelUtil.ReadData(xssfSheet, i, 1) ) ? 0 :Integer.parseInt( ExcelUtil.ReadData(xssfSheet, i, 1) ));
                scviewrecordmx.setJccode1(row.getCell(2).getStringCellValue());
                scviewrecordmx.setJccount1(StrUtil.isEmpty(ExcelUtil.ReadData(xssfSheet, i, 3) ) ? 0 :Integer.parseInt( ExcelUtil.ReadData(xssfSheet, i, 3) ));

                scviewrecordmx.setCccode2(row.getCell(4).getStringCellValue());
                scviewrecordmx.setCccount2(StrUtil.isEmpty(ExcelUtil.ReadData(xssfSheet, i, 5) ) ? 0 :Integer.parseInt( ExcelUtil.ReadData(xssfSheet, i, 5)));
                scviewrecordmx.setJccode2(row.getCell(6).getStringCellValue());
                scviewrecordmx.setJccount2(StrUtil.isEmpty(ExcelUtil.ReadData(xssfSheet, i, 7) ) ? 0 :Integer.parseInt( ExcelUtil.ReadData(xssfSheet, i, 7)));

                scviewrecordmx.setCccode3(row.getCell(8).getStringCellValue());
                scviewrecordmx.setCccount3(StrUtil.isEmpty(ExcelUtil.ReadData(xssfSheet, i, 9) ) ? 0 :Integer.parseInt( ExcelUtil.ReadData(xssfSheet, i, 9) ));
                scviewrecordmx.setJccode3(row.getCell(10).getStringCellValue());
                scviewrecordmx.setJccount3(StrUtil.isEmpty(ExcelUtil.ReadData(xssfSheet, i, 11) ) ? 0 :Integer.parseInt( ExcelUtil.ReadData(xssfSheet, i, 11) ));

                scviewrecordmx.setClcount1(ExcelUtil.ReadData(xssfSheet, i, 12));
                scviewrecordmx.setClcount2(ExcelUtil.ReadData(xssfSheet, i, 13));
                scviewrecordmx.setClcount3(ExcelUtil.ReadData(xssfSheet, i, 14));

                scviewrecordmx.setCqr1(row.getCell(15).getStringCellValue());
                scviewrecordmx.setCqr2(row.getCell(16).getStringCellValue());
                scviewrecordmx.setCqr3(row.getCell(17).getStringCellValue());

                scviewrecordmx.setCdefine1(row.getCell(18).getStringCellValue());
                scviewrecordmx.setCdefine2(row.getCell(19).getStringCellValue());
                scviewrecordmxList.add(scviewrecordmx);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("解析EXCEL失败:" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public int addScviewrecord(Scviewrecord record, List<Scviewrecordmx> scviewrecordmxList) {
        scviewrecordDao.insert(record);
        for (Scviewrecordmx scviewrecordmx : scviewrecordmxList) {
            scviewrecordmx.setRid(record.getId());
            scviewrecordmxDao.insert(scviewrecordmx);
        }
        return 0;
    }

    @Override
    public int updateScviewrecord(Scviewrecord record) {
        return scviewrecordDao.updateByPrimaryKey(record);
    }

    @Transactional
    @Override
    public int deleteScviewrecord(Integer id) {
        scviewrecordmxDao.deleteByPrimaryKey(id);
        return scviewrecordDao.deleteByPrimaryKey(id);
    }

    @Override
    public Scviewrecord selectScviewrecordById(Integer id) {
        return scviewrecordDao.selectByPrimaryKey(id);
    }

    @Override
    public List<ScviewrecordVo> selectScviewrecordByCondition(Integer orgId) {
        return scviewrecordDao.selectScviewrecordByCondition(orgId);
    }
}
